<?php

/**
 * SvcPHP
 *
 * Copyright (C) 2012 João Francisco Biondo Trinca
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * @package    SvcPHP
 * @author     João Francisco Biondo Trinca <wolfulus@gmail.com>
 * @copyright  2012 João Francisco Biondo Trinca <wolfulus@gmail.com>
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
 * @link       http://code.google.com/p/svcphp/
 *
 */

/**
 * Implements the template vars class.
 */
class Svc_Template_Data
{
    /**
     * Stores all template variables.
     *
     * @var array
     */
    private $_variables = array();

    /**
     * Stores all template functions.
     *
     * @var array
     */
    private $_functions = array();

    /**
     * Sets a template variable.
     *
     * @param mixed $name
     * @param mixed $value
     *
     * @return void
     */
    public function assign($variable, $value = null)
    {
        if (is_array($variable))
        {
            $this->_variables += $variable;
        }
        else
        {
            $this->_variables[$variable] = $value;
        }
    }

    /**
     * Clears all template variables.
     *
     * @return void
     */
    public function clear()
    {
        $this->_variables = array();
    }

    /**
     * Registers a function.
     *
     * @param mixed $name
     * @param callback $function
     *
     * @return void
     */
    public function register($name, $function)
    {
        if (is_callable($function, false))
        {
            $this->_functions[$name] = $function;
        }
    }

    /**
     * Gets a variable.
     *
     * @param mixed $name
     *
     * @return mixed
     */
    public function __get($name)
    {
        if (isset($this->_variables[$name]))
        {
            return $this->_variables[$name];
        }

        return null;
    }

    /**
     * Gets a variable.
     *
     * @param mixed $name
     *
     * @return void
     */
    public function __set($name, $value)
    {
        if (is_callable($value))
        {
            $this->_functions[$name] = $value;
        }
        else
        {
            $this->_variables[$name] = $value;
        }
    }

    /**
     * Checks if a variable exists.
     *
     * @param mixed $name
     *
     * @return bool
     */
    public function __isset($name)
    {
        return isset($this->data[$name]);
    }

    /**
     * Calls a function.
     *
     * @param mixed $name
     * @param mixed $arguments
     *
     * @return mixed
     */
    public function __call($name, $arguments)
    {
        if (isset($this->_functions[$name]))
        {
            $func = $this->_functions[$name];

            return call_user_func_array($func, $arguments);
        }
        else
        {
            if (function_exists($name))
            {
                return call_user_func_array($name, $arguments);
            }
            else
            {
                throw new Svc_Exception(Svc_Exception::TEMPLATE_FUNCTION_NOT_FOUND, $name);
            }
        }

        return;
    }

    /**
     * Renders a template file.
     *
     * @param mixed $file
     */
    public function render($file)
    {
        ob_start();
        include($file);
        $output = ob_get_clean();

        return $output;
    }

}